setScreenMetrics(device.width, device.height);
let current_package = "";
function launchAppEnhance(app_name) {
    let thread = threads.start(function () {
        toastLog("检查是否有打开权限!");
        while (true) {
            var allow = text("允许").findOne(5000);
            if (allow) {
                sleep(1000)
                toastLog("发现允许授权!");
                allow.click();
                sleep(1000);
                thread.interrupt();
                break;
            }
        }
    });
    sleep(1000);
    var result = launchApp(app_name);
    sleep(3000);
    thread.interrupt();
    return result;
}


app_func = {}
app_func.module_name = "转转自动签到";

app_func.main = function () {
    var open_zhuanzhuan = launchAppEnhance("转转");
    if (open_zhuanzhuan) {
        toastLog("正在为您打开转转");
    }
    else {
        toastLog("应用转转没有安装！退出");
        return false;
    }
    sleep(10000);

    //关闭更新提示
    let update1 = text("下次再说").findOne(2000);
    if (update1) {
        log("发现更新提醒");
        update1.click();
        sleep(1000);
    }

    let update2 = text("残忍拒绝").findOne(2000);
    if (update2) {
        log("发现更新提醒");
        update2.click();
        sleep(1000);
    }

    //关闭刚打开时的弹窗
    var close = id("q1").findOne("3000");
    if (close) {
        toastLog("发现弹窗，关闭");
        sleep(1000);
        close.click();
    }

    //关闭 好评弹窗
    var close = id("jk").findOne("3000");
    if (close) {
        toastLog("发现弹窗，关闭");
        sleep(1000);
        close.click();
    }

    //关闭 好评弹窗
    var close = id('com.wuba.zhuanzhuan:id/l_').findOne("3000")
    if (close) {
        toastLog("发现弹窗，关闭");
        sleep(1000);
        close.click();
    }

    //关闭 好评弹窗
    var close = id("pv").findOne("3000");
    if (close) {
        toastLog("发现弹窗，关闭");
        sleep(1000);
        close.click();
    }

    //点击领现金
    var lingxianjin = id("cbr").findOne(4000);
    if (lingxianjin) {
        toastLog("找到领现金！开始点击");
        lingxianjin.click();
        sleep(10000);
        //看5次商品领现金
        sleep(3000);
        swipe(device.width / 2, device.height / 2, device.width / 2, device.height / 4, random(700, 1500));
        var obj = textStartsWith("今日点击次数").findOne(1000);
        if (obj) {
            var count = obj.parent().child(0).text();
            if (count == '5') {
                toastLog("今天已经点击过了！");
                return;
            }
        }

        var tuijian = text("推荐").findOne(2000);
        var click_x, click_y;
        if (tuijian) {
            click_x = tuijian.bounds().centerX();
            click_y = tuijian.bounds().centerY() + 600;
            sleep(1000);
            click(click_x, click_y);
            sleep(3000);
            back();
            var gongxi = text("恭喜获得现金").findOne(2000);
            if (gongxi) {
                var obj = gongxi.parent().child(gongxi.parent().childCount() - 1);
                sleep(1000);
                obj.click();
            }
        }
        else {
            toastLog("Not found tuijian");
        }
        var lan_obj = text("男鞋").findOne(2000);
        if (lan_obj) {
            sleep(1000);
            lan_obj.click();
            sleep(3000)
            click(click_x, click_y);
            sleep(3000);
            back();
            var gongxi = text("恭喜获得现金").findOne(2000);
            if (gongxi) {
                var obj = gongxi.parent().child(gongxi.parent().childCount() - 1);
                sleep(1000);
                obj.click();
            }

        }
        else {
            toastLog("Not found nanxie");
        }

        var lan_obj = text("衣帽").findOne(2000);
        if (lan_obj) {
            sleep(1000);
            lan_obj.click();
            sleep(3000)
            click(click_x, click_y);
            sleep(3000);
            back();
            var gongxi = text("恭喜获得现金").findOne(2000);
            if (gongxi) {
                var obj = gongxi.parent().child(gongxi.parent().childCount() - 1);
                sleep(1000);
                obj.click();
            }

        }
        else {
            toastLog("Not found 衣帽");
        }

        var lan_obj = text("运动鞋服").findOne(2000);
        if (lan_obj) {
            sleep(1000);
            lan_obj.click();
            sleep(3000)
            click(click_x, click_y);
            sleep(3000);
            back();
            var gongxi = text("恭喜获得现金").findOne(2000);
            if (gongxi) {
                var obj = gongxi.parent().child(gongxi.parent().childCount() - 1);
                sleep(1000);
                obj.click();
            }

        }
        else {
            toastLog("Not found 运动鞋服");
        }

        var lan_obj = text("女包").findOne(2000);
        if (lan_obj) {
            sleep(1000);
            lan_obj.click();
            sleep(3000)
            click(click_x, click_y);
            sleep(3000);
            back();
            var gongxi = text("恭喜获得现金").findOne(2000);
            if (gongxi) {
                var obj = gongxi.parent().child(gongxi.parent().childCount() - 1);
                sleep(1000);
                obj.click();
            }

        }
        else {
            toastLog("Not found 女包");
        }
    }
    else {
        toastLog("没有找到领现金！");
    }

}

try {
    app_func.main();
} catch (error) {
    toastLog(error);
}

var storage = storages.create("xiangxiangyuedu");
storage.put("script_running_status", "end");

